package com.hongxia.assetsys.controller;

import com.hongxia.assetsys.common.BaseResponse;
import com.hongxia.assetsys.common.ErrorCode;
import com.hongxia.assetsys.common.ResultUtil;
import com.hongxia.assetsys.domain.AssertOrg;
import com.hongxia.assetsys.domain.Asset;
import com.hongxia.assetsys.domain.TaskAttack;
import com.hongxia.assetsys.exception.BusinessException;
import com.hongxia.assetsys.service.TaskAttackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController()
@RequestMapping("/taskattack")
@RequiresRoles(value = {"asset manager", "admin"}, logical = Logical.OR)
@Slf4j
public class TaskAttackController {
    @Resource
    TaskAttackService taskAttackService;

    @PostMapping
    public BaseResponse<Integer> addTaskAttack(@RequestBody TaskAttack task){
        if(task == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Integer id = taskAttackService.addTaskAttack(task);
        return ResultUtil.success(id);
    }

    @GetMapping("/list")
    public BaseResponse<List<TaskAttack>> getTaskAttackList(){
        List<TaskAttack> taskList = taskAttackService.list();
        return ResultUtil.success(taskList);
    }

    @GetMapping("/{id}")
    public BaseResponse<TaskAttack> getTaskAttackyId(@PathVariable Integer id){
        if(id < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TaskAttack task = taskAttackService.getById(id);
        return ResultUtil.success(task);
    }

    @GetMapping("/search/name")
    public BaseResponse<List<TaskAttack>> getTaskByTaskNameLikeyly(String taskname){
        List<TaskAttack> taskList = taskAttackService.getTaskByTaskNameLikeyly(taskname);
        return ResultUtil.success(taskList);
    }

    @GetMapping("/search/desc")
    public BaseResponse<List<TaskAttack>> getTaskByTaskDescLikeyly(String taskdesc){
        List<TaskAttack> taskList = taskAttackService.getTaskByTaskDescLikeyly(taskdesc);
        return ResultUtil.success(taskList);
    }

    @GetMapping("/search/status")
    public BaseResponse<List<TaskAttack>> getTaskByStatus(String taskstatus){
        List<TaskAttack> taskList = taskAttackService.getTaskByStatus(taskstatus);
        return ResultUtil.success(taskList);
    }

    @PutMapping("/status")
    public BaseResponse<Boolean> startTask(Integer id){
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = taskAttackService.beginTask(id);
        return ResultUtil.success(result);
    }

    @PutMapping("/assign")
    public BaseResponse<Boolean> assignTask(Integer id, String tester){
        if(id == null || tester == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = taskAttackService.assignTask(id,tester);
        return ResultUtil.success(result);
    }

    @PutMapping("/end")
    public BaseResponse<Boolean> endTask(Integer id){
        if(id == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = taskAttackService.endTask(id);
        return ResultUtil.success(result);
    }
}
